package app;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class NumericAttribute extends Attribute {
	private NumericValue minValue, maxValue;
	private List<NumericValue> values;
	private NumericValue splitValue;

	public NumericAttribute() {
		this.type = AttributeType.NUMERIC;
		minValue = new NumericValue(Double.NaN);
		maxValue = new NumericValue(Double.NaN);
		
		this.values = new ArrayList<NumericValue>();
	}
	
	public NumericAttribute(String name) {
		this();
		this.name = name;
	}
	
	public NumericValue getMinValue() {
		return minValue;
	}
	
	public NumericValue getMaxValue() {
		return maxValue;
	}
	
	public List<NumericValue> getValues() {
		return values;
	}
	
	public NumericValue getValue(int index) {
		return values.get(index);
	}
	
	public void addValue(NumericValue value) {
		double doubleMinValue = minValue.getValue();
		double doubleMaxValue = maxValue.getValue();
		double doubleValue = value.getValue();
		
		// TODO: shouldn't we check if the value already exists?!
//		if(!values.contains(value))
			values.add(value);
		
		if(Double.isNaN(doubleMinValue) && Double.isNaN(doubleMaxValue)) {
			minValue = maxValue = value;
		} else if(doubleMinValue > doubleValue) {
			minValue = value;
		} else if(doubleMaxValue < doubleValue) {
			maxValue = value;
		}
	}
	
	public NumericValue addValue(double doubleValue) {
		// TODO: shouldn't we check if the value already exists?!
/*
		for (NumericValue value : values) {
			if(doubleValue == value.getValue())
				return value;
		}
*/
		NumericValue newValue = new NumericValue(doubleValue);
		double doubleMinValue = minValue.getValue();
		double doubleMaxValue = maxValue.getValue();
		
		values.add(newValue);
		
		if(Double.isNaN(doubleMinValue) && Double.isNaN(doubleMaxValue)) {
			minValue = maxValue = newValue;
		} else if(doubleMinValue > doubleValue) {
			minValue = newValue;
		} else if(doubleMaxValue < doubleValue) {
			maxValue = newValue;
		}
		
		return newValue;	
	}
	
	public String toString() {
		return name;
	}
	
	public int noValues() {
		return values.size();
	}
	
	public NumericValue getSplitValue() {
		return splitValue;
	}

	public void setSplitValue(NumericValue splitValue) {
		this.splitValue = splitValue;
	}
} 